package com.androidhive.jsonparsing;

import com.androidhive.jsonparsing.MyLocation.LocationResult;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Starter extends Activity{
	
	private Thread mSplashThread;  
    private static final String TAG = "Debug of Ohad's Project"; // 
	
	public void onCreate(Bundle paramBundle)
	    {
	        super.onCreate(paramBundle);
	        setContentView(R.layout.starter);
	       		    
	        LocationResult locationResult = new LocationResult(){
	            @Override
	            public void gotLocation(Location location)
	            {
    	            if(location == null)
    	            {
    	            	Toast.makeText(
    	            			getApplicationContext(),
    	            			"GPS koordinator " + "\n ikke fundet...!" ,
    	            			Toast.LENGTH_SHORT).show();	  
    	            }

	                SQLiteDatabase db = openOrCreateDatabase("mydb.db", Context.MODE_PRIVATE, null);
	                db.execSQL("CREATE TABLE IF NOT EXISTS gps_kordinater (ID INTEGER PRIMARY KEY AUTOINCREMENT, Latitude REAL, Longitude REAL);");

	                ContentValues gps_values = new ContentValues();
	                

	                gps_values.put("Latitude", location.getLatitude());
	                gps_values.put("Longitude", location.getLongitude());
	   

	                 try {
	                     db.beginTransaction();
	                     db.insert("gps_kordinater", null, gps_values);
	                     db.setTransactionSuccessful();
	                 } finally {
	                     db.endTransaction();
	                 }
	                 
                     	Toast.makeText(
	            			getApplicationContext(),
	            			"GPS koordinator " + "\n Fundet!" ,
	            			Toast.LENGTH_SHORT).show();	  

	            }
	            
	            
	           };
	        MyLocation myLocation = new MyLocation();
	        
	        myLocation.getLocation(this, locationResult);
	        
	       final Starter sPlashScreen = this; 
	           
	        mSplashThread =  new Thread(){
    @Override
    public void run(){
        try {
        	synchronized(this){
                wait(5000);
                
            }
        }
        	catch(InterruptedException ex){                    
        }

        finish();
        
        Intent intent = new Intent();
        intent.setClass(sPlashScreen,DashboardMain.class);
        startActivity(intent);
        //finish();                   
    }
};

mSplashThread.start();        
}

// Processes splash screen touch events

@Override
 	public boolean onTouchEvent(MotionEvent evt) {
	
	if(evt.getAction() == MotionEvent.ACTION_DOWN)
	{
		synchronized(mSplashThread)
		{
        mSplashThread.notifyAll();
		}
	}
	return true;
  }
}

